package cn.bouncyslime.algorithms.stack.balanceSymbol;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;

public class BalanceSymbol2 {
	
	public int[] resolve() {
		Deque<Character> stack = new LinkedList<Character>();
		List<char[]> charArrayList = new ArrayList<>();
//		List<List<Character>> charList = new ArrayList<>();
		List<Character> openSymbolList = new ArrayList<Character>();
		List<Character> closedSymbolList = new ArrayList<Character>();
		openSymbolList.add('(');
		openSymbolList.add('[');
		openSymbolList.add('{');
		closedSymbolList.add(')');
		closedSymbolList.add(']');
		closedSymbolList.add('}');
		BufferedReader bufferedReader = null;
		try {
			bufferedReader = new BufferedReader(new FileReader("stack_BalanceSymbol.txt"));
			String line;
			while((line=bufferedReader.readLine())!=null) {
				char[] array = line.toCharArray();
				charArrayList.add(array);
			}
			
			char[][] charArray = charArrayList.toArray(new char[0][]);
			
			for (int i = 0; i < charArray.length; i++) {
				System.out.println(Arrays.toString(charArray[i]));
			}
			
			for (int i = 0; i < charArray.length; i++) {
				for (int j = 0; j < charArray[i].length; j++) {
					if (stack.isEmpty() && closedSymbolList.contains(charArray[i][j])) {
						System.out.println("出错，没有开放符号");
						return new int[] {i,j};
					}else if(openSymbolList.contains(charArray[i][j])) {
						stack.push(charArray[i][j]);
					}else if(closedSymbolList.contains(charArray[i][j])) {
						char bchar = stack.pop();
						if (openSymbolList.indexOf(bchar) != closedSymbolList.indexOf(charArray[i][j])) {
							System.out.println("报错，封闭符号与开放符号不对应");
							return new int[] {i,j};
						}
					}
				}
			}
			if(!stack.isEmpty()) {
				System.out.println("出错，没有封闭符号");
				return new int[] {charArray.length,charArray[charArray.length-1].length};
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				bufferedReader.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return null;
	}
	
	public static void main(String[] args) {
		BalanceSymbol2 b = new BalanceSymbol2();
		System.out.println(Arrays.toString(b.resolve()));
	}
}
